package com.yjz.zpay.ui;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.yjz.zpay.R;
import com.yjz.zpay.utils.AES;
import com.yjz.zpay.utils.L;
import com.yjz.zpay.utils.SpUtils;

import org.json.JSONObject;

/**
 * setting
 *
 * @author yjz
 *         created at 2019/1/18 下午9:43
 */

public class SettingActivity extends Activity implements OnClickListener {

    private EditText /*et_notifyurl,et_no,et_key*/ et_content;
    private Button bt_save, bt_back;
    private RelativeLayout rl_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_setting);
        /*et_notifyurl=(EditText) findViewById(R.id.et_notifyurl);
        if(!TextUtils.isEmpty(SpUtils.getString(getApplicationContext(), "notifyurl"))){
			et_notifyurl.setText(SpUtils.getString(getApplicationContext(), "notifyurl"));
		}
		et_no=(EditText) findViewById(R.id.et_no);
		if(!TextUtils.isEmpty(SpUtils.getString(getApplicationContext(), "no"))){
			et_no.setText(SpUtils.getString(getApplicationContext(), "no"));
		}
		et_key=(EditText) findViewById(R.id.et_key);
		if(!TextUtils.isEmpty(SpUtils.getString(getApplicationContext(), "key"))){
			et_key.setText(SpUtils.getString(getApplicationContext(), "key"));
		}*/


        et_content = findViewById(R.id.et_content);
        bt_save = (Button) findViewById(R.id.save);
        bt_back = (Button) findViewById(R.id.back);
        rl_back = (RelativeLayout) findViewById(R.id.rl_back);
        bt_back.setOnClickListener(this);
        bt_save.setOnClickListener(this);
        rl_back.setOnClickListener(this);

        //TODO
        String data = "6F170DCB234F5E1EB1D541EB6457B112D32D4A6FF547CEF54CDCC59786A67E8941BFEDBC8957F8BEF057409D713C78502F38E8E95F7CC0B4923964D9F1FA31E6256EB2ABA5F7A0E4384749FF74F4575C10DBC4F91EB2C1D6BA37E7EC3BF59711";
        et_content.setText(data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save:
            /*String notifyurl = et_notifyurl.getText().toString();
            String key=et_key.getText().toString();
			String no=et_no.getText().toString();
			if(TextUtils.isEmpty(notifyurl)){
				Toast.makeText(getApplicationContext(), "异步通知地址不能为空！", Toast.LENGTH_LONG).show();
				return;
			}else{
				SpUtils.putString(getApplicationContext(), "notifyurl", notifyurl);
			}
			if(TextUtils.isEmpty(no)){
				Toast.makeText(getApplicationContext(), "设备号不能为空！", Toast.LENGTH_LONG).show();
				return;
			}else{
				SpUtils.putString(getApplicationContext(), "no", no);
			}
			if(TextUtils.isEmpty(key)){
				Toast.makeText(getApplicationContext(), "signKey不能为空！", Toast.LENGTH_LONG).show();
				return;
			}else{
				SpUtils.putString(getApplicationContext(), "key", key);
			}

			Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show();
			finish();*/


                String data = et_content.getText().toString();
                if (TextUtils.isEmpty(data)) {
                    Toast.makeText(this, "请先输入内容", Toast.LENGTH_SHORT).show();
                    return;
                }


                try {
                    String decodeData = AES.decode(data);
                    JSONObject jsonObjs = new JSONObject(decodeData);
                    String host = jsonObjs.getString("host");
                    String userId = jsonObjs.getString("userId");
                    String applyId = jsonObjs.getString("applyId");
                    String key = jsonObjs.getString("key");

                    L.d(host + "\n" + userId + "\n" + applyId + "\n" + key);

                    if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(userId)
                            && !TextUtils.isEmpty(applyId) && !TextUtils.isEmpty(key)) {
                        SpUtils.putString(getApplicationContext(), "host", host);
                        SpUtils.putString(getApplicationContext(), "agencyId", userId);
                        SpUtils.putString(getApplicationContext(), "appId", applyId);
                        SpUtils.putString(getApplicationContext(), "key", key);

                        Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                } catch (Exception e) {
                    Toast.makeText(this, "输入内容有误,请检查后重新输入", Toast.LENGTH_SHORT).show();
                }


                break;
            case R.id.back:
                finish();
                break;
            case R.id.rl_back:
                finish();
                break;
            default:
                break;
        }
    }
}
